/**
 * User路由模块
 */

/**
 * @swagger
 * tags:
 *   - name: 用户路由
 *     description: 用户管理相关操作
 * 
 * components:
 *   schemas:
 *     User:
 *       required:
 *         - id
 *         - username
 *         - password
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *           format: int64
 *           example: 10
 *           description: 用户ID
 *         username:
 *           type: string
 *           description: 用户名
 *         password:
 *           type: string
 *           description: 密码（出于安全考虑，实际场景中不建议在API文档中直接写出此字段）
 *         nickname:
 *           type: string
 *           description: 昵称
 *         truename:
 *           type: string
 *           description: 姓名
 *         avatar:
 *           type: string
 *           description: 头像链接
 *         role:
 *           type: integer
 *           description: 用户角色
 *         lastLoginTime:
 *           type: string
 *           format: date-time
 *           description: 最后登录时间
 *         lastLoginIP:
 *           type: string
 *           description: 最后登录IP地址
 *         loginCount:
 *           type: integer
 *           description: 登录次数
 *         createdTime:
 *           type: string
 *           format: date-time
 *           description: 账户创建时间
 *         createdIP:
 *           type: string
 *           description: 账户创建时的IP地址
 *         updatedTime:
 *           type: string
 *           format: date-time
 *           description: 账户最后更新时间
 */

var express = require('express');
var router = express.Router();

const user = require("../controller/user");

const auth = require("../controller/auth");

/**
 * @swagger
 * /users/register:
 *   post:
 *     tags:
 *       - 用户路由
 *     summary: 用户注册
 *     description: 提供用于用户注册的API
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/User"
 *     responses:
 *       200:
 *         description: "注册成功，返回响应数据"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   additionalProperties: true # 如果"data"属性包含任意类型的数据，可以这样设置；如果有具体的结构，请替换为实际结构
 */
router.post("/register", user.register);

/**
 * @swagger
 * /users/login:
 *   post:
 *     tags:
 *       - 用户路由
 *     summary: 用户登录
 *     description: 用户执行登录操作
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 example: user01
 *               password:
 *                 type: string
 *                 example: '111111' # 示例密码通常会掩码或者使用占位符，这里假设明文示例仅作演示
 *     responses:
 *       200:
 *         description: "登录成功，返回响应数据"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   additionalProperties: true # 如果"data"字段为字符串以外的复杂结构，请在此处详细描述其属性
 */
router.post("/login", user.login);

/**
 * @swagger
 * /users/logout:
 *   get:
 *     tags:
 *       - 用户路由
 *     summary: 用户退出登录
 *     description: 用户执行登出操作
 *     responses:
 *       200:
 *         description: "登出成功，返回响应数据"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   additionalProperties: true # 如果"data"字段包含复杂结构，请在此处详细描述其属性
 */
router.get("/logout", auth.isLogin, user.logout);

/**
 * @swagger
 * /users/change-info/{id}:
 *   patch:
 *     tags:
 *       - 用户路由
 *     summary: 修改指定用户的个人信息
 *     description: 更新用户的信息，并返回受影响的记录数量
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 要更新的用户的ID
 *         schema:
 *           type: string
 *           format: uuid # 根据实际情况选择合适的格式，例如uuid、int64等
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               nickname:
 *                 type: string
 *               truename:
 *                 type: string
 *               avatar:
 *                 type: string
 *     responses:
 *       200:
 *         description: "更新成功，返回响应数据"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.patch("/change-info", auth.isLogin, user.changeInfo);

/**
 * @swagger
 * /users/change-password/{id}:
 *   patch:
 *     tags:
 *       - 用户路由
 *     summary: 修改指定用户的密码
 *     description: 更新用户密码，并返回受影响的记录数量
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         description: 要更改密码的用户的ID
 *         schema:
 *           type: string
 *           format: uuid # 根据实际情况选择合适的格式，例如uuid、int64等
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               password:
 *                 type: string
 *                 format: password # 可选，用于强调这是一个密码字段
 *                 minLength: 8 # 添加密码最小长度限制（可选）
 *                 maxLength: 128 # 添加密码最大长度限制（可选）
 *                 description: 新的密码
 *     responses:
 *       200:
 *         description: "密码更新成功，返回响应数据"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.patch("/change-password", auth.isLogin, user.changePassword);

/**
 * @swagger
 * /users/check-password:
 *   patch: 
 *     tags: 
 *       - 用户路由
 *     summary: 修改用户密码
 *     description: 返回影响的记录数
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               password:
 *                 type: string
 *       required: true
 *     responses:
 *       200: 
 *         description: "Web成功访问"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: string
 * 
*/
router.post("/check-password", auth.isLogin, user.checkPassword);


/**
 * @swagger
 * /users/reset-password/{id}:
 *   patch:
 *     tags:
 *       - 用户路由
 *     summary: 重置指定用户的密码
 *     description: 重置用户密码并返回受影响的记录数量
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64 # 根据实际情况选择合适的整数格式，例如int32、int64等
 *         description: 要重置密码的用户的内部ID
 *     responses:
 *       200:
 *         description: "密码重置成功，返回响应数据"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.patch("/reset-password/:id", auth.isLogin, auth.isAdmin, user.resetPassword);

/**
 * @swagger
 * /users/count:
 *   get:
 *     tags:
 *       - 用户路由
 *     summary: 获取用户总数
 *     description: 返回系统中用户的总数量
 *     responses:
 *       200:
 *         description: "请求成功，返回用户总数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: integer
 *                       description: 用户总数
 */
router.get("/count", auth.isLogin, auth.isAdmin, user.getCount);

/**
* @swagger
* /users/search:
*   get:
*     tags:
*       - 用户路由
*     summary: 根据关键字搜索用户列表信息
*     description: 返回匹配查询关键字的所有用户列表信息
*     parameters:
*       - name: q
*         in: query
*         description: 搜索关键字
*         required: true
*         schema:
*           type: string
*       - name: limit
*         in: query
*         description: 返回结果的数量限制
*         required: false
*         schema:
*           type: integer
*       - name: offset
*         in: query
*         description: 结果集起始位置偏移量
*         required: false
*         schema:
*           type: integer
*     responses:
*       200:
*         description: "请求成功，返回用户列表信息"
*         content:
*           application/json:
*             schema:
*               type: object
*               properties:
*                 code:
*                   type: integer
*                   format: int32
*                 msg:
*                   type: string
*                 data:
*                   type: array
*                   items:
*                     $ref: '#/components/schemas/User'
*/
router.get("/search", auth.isLogin, auth.isAdmin, user.search);

/**
 * @swagger
 * /users/{id}:
 *   get:
 *     tags:
 *       - 用户路由
 *     summary: 获取指定ID的用户信息
 *     description: 根据用户内部ID返回用户详细信息
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 要查询的用户的内部ID
 *     responses:
 *       200:
 *         description: "请求成功，返回用户信息"
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/User'
 */
router.get("/:id", auth.isLogin, auth.isSelfOfAdmin, user.find);

/**
 * @swagger
 * /users:
 *   get:
 *     tags:
 *       - 用户路由
 *     summary: 获取用户列表信息
 *     description: 返回系统中的所有用户列表信息，可按需指定查询数量和偏移量
 *     parameters:
 *       - name: limit
 *         in: query
 *         description: 返回结果的数量限制，默认返回全部
 *         required: false
 *         schema:
 *           type: integer
 *       - name: offset
 *         in: query
 *         description: 结果集起始位置偏移量，默认为0
 *         required: false
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: "请求成功，返回用户列表信息"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/User'
 */
router.get("/", auth.isLogin, auth.isAdmin, user.findAll);

/**
 * @swagger
 * /users:
 *   post:
 *     tags:
 *       - 用户路由
 *     summary: 新增用户信息
 *     description: 添加新的用户信息并返回新增用户的ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/UserSchema"
 *     responses:
 *       200:
 *         description: "请求成功，返回新增用户的ID"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: string
 *                   description: 新增用户的ID
 */
router.post("/", auth.isLogin, auth.isAdmin, user.add);

/**
 * @swagger
 * /users/{id}:
 *   put:
 *     tags:
 *       - 用户路由
 *     summary: 修改指定用户信息
 *     description: 更新指定用户的详细信息，并返回受影响的记录数
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 需要更新的用户的内部ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             $ref: "#/components/schemas/User"
 *     responses:
 *       200:
 *         description: "请求成功，返回受影响的记录数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.put("/:id", auth.isLogin, auth.isAdmin, user.update);

/**
 * @swagger
 * /users/{id}:
 *   delete:
 *     tags:
 *       - 用户路由
 *     summary: 删除指定用户信息
 *     description: 根据用户的内部ID删除用户信息，并返回受影响的记录数
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *           format: int64
 *         description: 待删除用户的内部ID
 *     responses:
 *       200:
 *         description: "请求成功，返回受影响的记录数"
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: integer
 *                   format: int32
 *                 msg:
 *                   type: string
 *                 data:
 *                   type: integer
 *                   description: 受影响的记录数量
 */
router.delete("/:id", auth.isLogin, auth.isAdmin, user.remove);

module.exports = router;